/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.SyncBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.SynchController;
import org.ipti.br.tag.entity.Unity;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import org.ipti.br.tag.util.SynchronizationFile;

/**
 *
 * @author israel
 */
@ManagedBean(name = "synchronizationBean")
@RequestScoped
public class SynchronizationBean extends AppBaseBean {

    @Inject
    private SyncBO synchronizationBO;
    @Inject
    private UnityBO unityBO;

    public SynchController getServer() {
        return server;
    }
    
    public void setServer(SynchController server) {
        this.server = server;
    }
    private List<SynchronizationFile> filesList;
    private List<SynchController> serverList;
    private SynchController server;

    public SynchronizationFile getFile() {
        return file;
    }

    public void setFile(SynchronizationFile file) {
        this.file = file;
    }
    private SynchronizationFile file;

    public List<SynchController> getServerList() {
        loadServerList();
        //teste
        return serverList;
    }

    public void setServerList(List<SynchController> serverList) {
        this.serverList = serverList;
    }
    
    public void init(){
        loadFilesList();
        loadServerList();
    }
    
    public String findUnityName(Long unityID) {
        //System.out.print(unityID);
        Unity unityAux = unityBO.find(unityID);
        return unityAux.getName();
    }
    
    public void loadFilesList() {
        filesList = new ArrayList<SynchronizationFile>();
        for (String s : synchronizationBO.loadFilesList()) {
            filesList.add(new SynchronizationFile(s, false));
        }
    }
    
    public void loadServerList(){
        serverList = new ArrayList<SynchController>();
        for (SynchController s : synchronizationBO.getSynchControllerDAO().findAll()) {
            
            serverList.add(s);
        }
    }

    public void processFiles() {
        for (SynchronizationFile sf : filesList) {
            if (sf.isOk()) {
                Long serial = synchronizationBO.readSynchronizationZip(sf.getName());
                synchronizationBO.createSynchronizationFile(serial);
            }
        }
    }

    public void createSynchronizationFile() {
         try {
            synchronizationBO.createSynchronizationFile(server.getSerialNumber());
            addInfoMessage(null, "Arquivo gerado com sucesso.", null);
        } catch (Exception e) {
           addErrorMessage(null, "Problemas na leitura do arquivo, detalhes do erro:"+e.getMessage(), null);
        }
    }

    public void readSynchronizationFile() {
        try {
            Long serial = synchronizationBO.readSynchronizationZip(this.getFile().getName());
            if(serial != null){
                addInfoMessage(null, "Sincronização realizada com sucesso.", null);
            } else {
                addErrorMessage(null, "Servidor Malvado! :/", "Contate o administrador do sistema");
            }
        } catch (Exception e) {
           addErrorMessage(null, "Erro na Sincronização", "detalhes do erro: "+e.getMessage());
        }
    }

    public List<SynchronizationFile> getFilesList() {
        if (filesList == null) {
            loadFilesList();
        }
        return filesList;
    }

    public void setFilesList(List<SynchronizationFile> filesList) {
        this.filesList = filesList;
    }
}
